<template>
    <div>
    <div v-for="(comment, index) in comments" :key="index" class="comment-item">
        <img :src="GetHeadImage(comment.head)" class="head" />
        <div class="comment-content">
          <div class="user-info">
            <span class="username">{{ comment.username }}</span>
            <span class="label">{{ comment.label }}</span>
          </div>
          <div class="comment-details">
            <span>{{ comment.detailMessage }}</span>
            <span class="timestamp">{{ comment.timestamp }}</span>
          </div>
          <div v-if="comment.isReplying" class="reply-box">
            <input type="text" v-model="comment.replyMessage" :placeholder="'回复 ' + comment.username" />
            <button @click="showEmojis(comment)">🙂</button>
            <transition name="slide" v-if="comment.replyMessage" >
              <button  class="send-button" @click="sendReply(comment)">发送</button>
            </transition>
            <button @click="cancelReply(comment)">取消</button>
          </div>
          <div class="action-buttons" v-else>
            <button @click="() => reply(comment)" class="reply">🗨️回复</button>
            <button @click="() => like(comment)" class="aixin">
              {{ comment.like === 1 ? "❤️" : "🤍" }}
            </button>
          </div>
        </div>
        <img src="@/assets/img/img02.jpg" class="zoom-icon" />
      </div>
    </div>  
    </template>
    
    <script setup>
    import { defineProps, ref } from 'vue';
    
    const props = defineProps({
      comments: {
        type: Array,
        required: true,
      },
    });
    const comments = ref(props.comments);
    const reply = (comment) => {
      comment.isReplying = true;
      comment.replyMessage = '';
    };
    
    const cancelReply = (comment) => {
      comment.isReplying = false;
    };
    
    const sendReply = (comment) => {
      console.log("发送回复给",comment.username,":", comment.replyMessage);
      comment.isReplying = false;
    };
    
    const showEmojis = (comment) => {
      // 显示表情选择窗口的逻辑
    };
    
    const like = (comment) => {
      comment.like = comment.like === 1 ? 0 : 1;
    };
    
    const GetHeadImage = (imgsrc) => new URL(imgsrc, import.meta.url).href;
    </script>
    
    <style scoped>
    .comment-item {
      display: flex;
      align-items: center;
      border-bottom: 1px solid #eee;
      padding: 10px 0;
    }
    .head {
      border-radius: 50%;
      width: 40px;
      height: 40px;
    }
    .comment-content {
      flex-grow: 1;
      margin-left: 10px;
    }
    .user-info {
      display: flex;
      align-items: center;
    }
    .label {
      float: left;
      background: rgba(0, 0, 0, 0.1);
      padding: 2px 5px;
      border-radius: 5px;
    }
    .action-buttons {
      display: flex;
      gap: 15px;
    }
    .reply-box {
      display: flex;
      align-items: center;
      width: 400px;
      height: 30px;
      position: relative;
    }
    .reply-box input{
      width: 500px;
      height: inherit;
      border-radius: 20px;
      border: 0.1px solid rgba(197, 194, 194, 0.1);
      background-color: #e8e6e6;
    }
    .reply-box button{
      width: 70px;
      height: inherit;
      border-radius: 20px;
      font-size: 10px;
      border: 0.1px solid rgba(0, 0, 0, 0.1);
      background-color: #fff;
      transition: background-color 1s ease; 
      margin-left: 5px;
    }
    
    .comment-item .send-button {
      background-color: rgb(210, 85, 73);
    }       
    .slide-enter-active, .slide-leave-active {
      transition: transform 2s ease;
    }
    .slide-enter-from{
      transform: translateX(100%);
    }
    .slide-leave-to{
      transform: translateX(-20px); /* 向右滑动 */
    }
    
    .reply-box button:hover {
      background-color: #adaaaa; /* 悬停时的背景颜色 */
    }
    .reply {
      border-radius: 10px;
      width: 70px;
      height: 30px;
      border: 0.1px solid rgba(0, 0, 0, 0.1);
      background-color: #fff;
    }
    
    </style>
    